<template>
<div class="container content-chat">
    <!-- py-4上间隔 -->
    <div>
      <div class="row">
        <div class="container-fluid themed-container chat-top text-center">豪华车之家用户聊天界面</div>
      </div>
      <div class="row mb-3">
        <div class="col-9 themed-grid-col clearmp">
          <div class="content-top">
            <!-- 消息区 -->
            <div class="lite-chatbox">
              <div class="cleft cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/mystyle/chat/img/A.jpg'"/><!-- ../../lib/chat/img/B.jpg -->
                <span class="name"><span class="htitle admin">admin</span>SuperPaxxs</span>
                <span class="content">欢迎光临,豪车之家平台，请问您有的话呢么要咨询的吗？</span>
              </div>
            </div>
            <div class="lite-chatbox">
              <div class="cright cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/mystyle/chat/img/B.jpg'"/>
                <span class="name">用户{{mId}}</span>
                <span class="content">{{mysend}}</span>
              </div>
            </div>
            <div class="lite-chatbox">
              <div class="cleft cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/mystyle/chat/img/A.jpg'"/><!-- ../../lib/chat/img/B.jpg -->
                <span class="name"><span class="htitle admin">admin</span>SuperPaxxs</span>
                <span class="content">{{info.content}}</span>
              </div>
            </div>
            <!-- <div v-for="(c,i) in charmessage" :key="i" v-html="c"></div> -->
          </div>
          <div class="content-bottom">
            <textarea class="message" id="message" cols="30" rows="10" placeholder="在此输入文本消息..." v-model="message" @keyup.enter="sendMessage"></textarea>
          </div>
          <button class="btn btn-primary" id="send" @click="sendMessage">发送</button>
        </div>
        <div class="col-3 themed-grid-col clearmp">
          <div class="question">
            常见问题
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return {
      webs:"",
      mId:1,
      message:"",
      info:{},
      mysend:""
    }
  },
  methods:{
    sendMessage(){
      let data = {
        userId: this.mId,
        uId: 5,
        content: this.message
      }
      this.webs.send(JSON.stringify(data));
      this.mysend = this.message;
      this.webs.onmessage = function (evt) {
        console.log(evt);
        // console.log(evt.data);
        this.info = JSON.parse(evt.data);
      }
      this.message="";
    },
    getConnention(){
      this.webs = new WebSocket('ws://localhost:80/koa/ws?id=' + this.mId);
      this.webs.onopen = function () {
        console.log('连接成功');
      }
      this.webs.onmessage = function (evt) {
        console.log(evt);
        try {
          this.info = JSON.parse(evt.data);
        } catch (error) {
          console.log(evt.data);
        }
        
        // console.log('连接==>'+evt.data);
      }
    }
  },
  created(){
    this.getConnention();
  },
  beforeDestroy(){
    this.onclose = function(){
      // 关闭 websocket
      alert("连接已关闭..."); 
    };
  }
}
</script>

<style scoped>
@import url("../../lib/mystyle/chat/litewebchat.min.css");
@import url("../../lib/bootstrap/bootstrap.min.css");
@import url("../../lib/bootstrap/grid.css");
html,body{
  height: 100%;
}
body{
  overflow: hidden;
}
.content-chat{
  height: 100%;
  padding: 0;
  margin: 0;
}
.chat-top{
  margin-bottom: 0px;
}
.content-top{
  height: 400px;
  min-height:400px;
}
.content-bottom{
  border-top: 1px solid rgba(86, 61, 124, .2);
  height: 150px;
  min-height:150px;
}
.question{
  height: 50px;
  border-bottom: 1px solid rgba(86, 61, 124, .2);
  line-height: 50px;
  padding-left: 20px;
}
.clearmp{
  margin: 0;
  padding: 0;
}
.message{
  width: 100%;
  height: 95%;
  border: none;
  resize: none;
  padding: 10px 5px;
  background-color: #E6E2EB;
}
.message:focus{
  outline: none;
}
#send{
  float: right;
}
</style>